﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3_c2_c3
{
    public static class ShapeFactory
    {
        //生成随机图形
        private static Random _random = new Random();

        //创建
        public static Shape CreateRandomShape()
        {
            //随机生成0-2的整数
            int type = _random.Next(3);
            //根据type值生成相应图形
            return type switch
            {
                0 => CreateRectangle(),
                1 => CreateSquare(),
                2 => CreateTriangle(),
                //其他值抛出异常
                _ => throw new InvalidOperationException()
            };
        }

        //创建矩形
        private static Shape CreateRectangle()
        {
            //随机生成长和宽
            Console.WriteLine("矩形");
            return new Rectangle(_random.NextDouble() * 10, _random.NextDouble() * 10);
        }

        //创建正方形
        private static Shape CreateSquare()
        {
            //随机生成边长
            Console.WriteLine("正方形");
            return new Square(_random.NextDouble() * 10);
        }

        //创建三角形
        private static Shape CreateTriangle()
        {
            //随机生成三边长
            double a, b, c;
            do
            {
                a = _random.NextDouble() * 10;
                b = _random.NextDouble() * 10;
                c = _random.NextDouble() * 10;
            } while (!(a + b > c && a + c > b && b + c > a));

            Console.WriteLine("三角形");
            return new Triangle(a, b, c);
        }

        public static void Main()
        {
            //创建10个随机图形
            List<Shape> shapes = new List<Shape>();
            double totalArea = 0;
            for (int i = 0; i < 10; i++)
            {
                shapes.Add(ShapeFactory.CreateRandomShape());
                if (shapes[i].IsValid())
                {
                    //计算单个图形的面积
                    Console.WriteLine(shapes[i].CalculateArea() + " ");

                    //计算总面积
                    totalArea += shapes[i].CalculateArea();
                }
            }

            Console.WriteLine($"面积和为: {totalArea}");
        }
    }
}
